import type { MeetingRoom } from "@/service/api/types/room";
import type { Requirement } from "@/service/api/types/reservation";
import type { Response } from "@/service/api/types/request";
import type { MockAdapterInstance } from "../index";
import _ from "lodash";

const generateMeetingRooms = (n: number): MeetingRoom[] => {
    return _.times(n, () => {
        const MeetingRoom: MeetingRoom = {
            id: _.uniqueId("room_"),
            name: `会议室-${_.random(1, 100)}`,
            area: _.random(20, 200),
            capacity: _.random(10, 100),
            type: _.sample(["classroom", "roundtable"]) as
                | "classroom"
                | "roundtable",
            device: {
                speaker: _.random(0, 1) === 1,
                internet: _.random(0, 1) === 1,
                projector: _.random(0, 1) === 1,
            },
            locked: _.random(0, 1) === 1,
            maintained: _.random(0, 1) === 1,
            hourlyRate: _.random(100, 1000),
            createdDate: Date.now() - _.random(0, 365 * 24 * 60 * 60 * 1000),
        };
        return MeetingRoom;
    });
};

const mockMeetingRooms = generateMeetingRooms(137);

export default function (mock: MockAdapterInstance) {
    mock.onPost("/room/get/list").reply((config) => {
        const response: Response<MeetingRoom[]> = {} as Response<MeetingRoom[]>;

        const { page, size } = JSON.parse(config.data);

        const start = (page - 1) * size;
        const end = start + size;

        response.code = 1;
        response.data = mockMeetingRooms.slice(start, end);

        return [200, response];
    });

    mock.onPost("/room/get/filter").reply((config) => {
        const response: Response<MeetingRoom[]> = {} as Response<MeetingRoom[]>;

        const requirement: Requirement = JSON.parse(config.data);

        response.code = 1;
        response.data = mockMeetingRooms;

        return [200, response];
    });
}
